/*
 *  Market.cpp
 *  SpaceTrader
 *
 *  Created by Andrew on 10/29/12.
 *  Copyright 2012 The Hex Pistols. All rights reserved.
 *
 */

#include "Market.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>

/* 
 * creates a new Market wich reads in data from a CSV file
 */
Market::Market() : itemMap(std::map<std::string, TradeGood*>()), itemList(std::vector<std::string>()) {
	
	std::ifstream file ("./itemInfo.csv");
	if (!file) std::cerr << "File could not be opened";
	
	std::string line; //delimited line of the CSV file
	std::string value, name, description, imageName;
	int basePrice, increasePerLevel, variance, minPrice, maxPrice;
	trade_good_t id;
	world_event_t event;
	resources_t cheap, expensive;
	tech_level_t minProduce, minUse, mostProduced;
	
	// using \r because, well, Microsoft
	while (std::getline(file, line, '\r')) {

		std::stringstream lineStream(line);
		
		std::getline(lineStream, name, ',');
		
		if ( name == "WATER" ) {
			id = static_cast<trade_good_t> (0);
		}
		else if ( name == "FURS" ) {
			id = static_cast<trade_good_t> (1);
		}
		else if ( name == "FOOD" ) {
			id = static_cast<trade_good_t> (2);
		}
		else if ( name == "ORE" ) {
			id = static_cast<trade_good_t> (3);
		}
		else if ( name == "GAMES" ) {
			id = static_cast<trade_good_t> (4);
		}
		else if ( name == "FIREARMS" ) {
			id = static_cast<trade_good_t> (5);
		}
		else if ( name == "MEDICINE" ) {
			id = static_cast<trade_good_t> (6);
		}
		else if ( name == "MACHINES" ) {
			id = static_cast<trade_good_t> (7);
		}
		else if ( name == "NARCOTICS" ) {
			id = static_cast<trade_good_t> (8);
		}
		else if ( name == "ROBOTS" ) {
			id = static_cast<trade_good_t> (9);
		}
		else {
			std::cerr << "Not a valid type: " << name;
			id = static_cast<trade_good_t> (0);
		}
		std::getline(lineStream, description, ',');
		
		std::getline(lineStream, imageName, ',');
		
		std::getline(lineStream, value, ',');
		minProduce = static_cast<tech_level_t> (atoi(value.c_str()));
		
		std::getline(lineStream, value, ',');
		minUse = static_cast<tech_level_t> (atoi(value.c_str()));
		
		std::getline(lineStream, value, ',');
		mostProduced = static_cast<tech_level_t> (atoi(value.c_str()));
		
		std::getline(lineStream, value, ',');
		basePrice = atoi(value.c_str());
		
		std::getline(lineStream, value, ',');
		increasePerLevel = atoi(value.c_str());
		
		std::getline(lineStream, value, ',');
		variance = atoi(value.c_str());
		
		std::getline(lineStream, value, ',');
		event = static_cast<world_event_t> (atoi(value.c_str()));
		
		std::getline(lineStream, value, ',');
		cheap = static_cast<resources_t> (atoi(value.c_str()));
		
		std::getline(lineStream, value, ',');
		expensive = static_cast<resources_t> (atoi(value.c_str()));
		
		std::getline(lineStream, value, ',');
		minPrice = atoi(value.c_str());
		
		std::getline(lineStream, value, ',');
		maxPrice = atoi(value.c_str());
		
		TradeGood* good = new TradeGood(name.c_str(), description.c_str(), imageName.c_str(), id, minProduce, minUse, mostProduced,
										basePrice, increasePerLevel, variance, event, cheap, expensive, minPrice, maxPrice);
		itemMap[name] = good;
		//std::cout << itemMap[name]->getBasePrice() << std::endl;
		itemList.push_back(name);
	}
}

Market::Market(const Market &M) : itemMap(M.itemMap), itemList(std::vector<std::string>(M.itemList.size()))
{
    for (unsigned int i = 0; i < itemList.size(); i++){
        itemList[i] = M.itemList[i];
    }
}

/*
 * returns a TradeGood from the item list
 */
TradeGood *Market::getTradeGood(std::string good) {
	return itemMap[good];
}

std::map<std::string, TradeGood*>* Market::getItemMap()
{
	return &itemMap;
}
std::vector<std::string> Market::getItemList()
{
	return itemList;
}
